package com.cms.manage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cms.manage.common.ConmonConfig;
import com.cms.manage.common.SysCode;
import com.cms.manage.dao.CmsUserMapper;
import com.cms.manage.model.CmsUser;
import com.cms.manage.service.LoginService;
import com.cms.manage.util.MD5Util;
import com.cms.manage.util.MapUtil;
import com.cms.manage.util.StringUtil;

/**
 * Created by sgy on 2017/4/7.
 */
@Service
public class LoginServiceImpl extends BaseService<CmsUser> implements LoginService {


    @Autowired
    private CmsUserMapper cmsUserMapper;
    

	/**
	 * 用户注册
	 * Created by sgy on 2020/6/9.
	 */
	@Override
	public Map<String, Object> register(Map<String, String> data) {
		Map<String, Object> result = ConmonConfig.getResultMap();	
		StringBuffer message = new StringBuffer(); 
		CmsUser cmsUser = new CmsUser();			
		//处理非空字段
		//注册名字不能为空
		if(StringUtils.isBlank(data.get("name"))){
			result.put("code",SysCode.error);
			message.append("用户名字不能为空! ");
		}else{
			cmsUser.setName(data.get("name"));
		}
		//判断是否有错误信息,如果有错位信息不判断是否存在用户
		 if(!(result.get("code")==SysCode.error)){
		   int count = cmsUserMapper.selectCount(cmsUser);//查询是否已经注册过
		   if(count>=1){//已经被注册直接返回
			   result.put("code",SysCode.error);
			   message.append("用户名字已经存在,不能重复注册！  ");
			   result.put("message", message.toString());
			   return result;
		   }
		 }
		//密码不能为空
		if(StringUtils.isBlank(data.get("password"))){
			result.put("code",SysCode.error);
			message.append("用户密码不能为空! ");			
		}else{
			cmsUser.setPassword(MD5Util.toMd5(data.get("password")));
		}		
		//可空字段处理
		cmsUser.setAge(MapUtil.getShort(data, "age"));//年龄
		cmsUser.setBark(data.get("bark"));//备注
		cmsUser.setCreatetime(new Date());//创建时间
		cmsUser.setSex(MapUtil.getShort(data,"sex"));//性别
		cmsUser.setHeadimg(data.get("headImg"));//头像
		cmsUser.setNikename(data.get("nikeName"));//昵称
		//判断是否有错误信息，如果没有进行注册操作
		 if(!(result.get("code")==SysCode.error)){
			 //注册数据 非空字段使用数据库默认值，注册为未激活用户
			 int count =  cmsUserMapper.insertSelective(cmsUser);
			 if(count<1){
				 result.put("code",SysCode.error);
				 message.append("网络异常,用户密码注册失败! ");		
			 }
		 }
		result.put("message", message.toString());
		return result;
	}
	/**
	 * 用户激活
	 * Created by sgy on 2020/6/9.
	 */
	@Override
	public Map<String, Object> activate(Map<String, String> data) {
		Map<String, Object> result = ConmonConfig.getResultMap();	
		StringBuffer message = new StringBuffer(); 
		CmsUser cmsUser = new CmsUser();	    
		//处理非空字段
		//用户名字不能为空
		if(StringUtils.isBlank(data.get("name"))){
			result.put("code",SysCode.error);
			message.append("用户名字不能为空! ");
		}else{
			cmsUser.setName(data.get("name"));
		}
		//密码不能为空
		if(StringUtils.isBlank(data.get("password"))){
		    result.put("code",SysCode.error);
			message.append("用户密码不能为空! ");			
		}else{
			cmsUser.setPassword(MD5Util.toMd5(data.get("password")));
		}
		//判断是否有错误信息,如果有错位信息不判断是否存在用户
		 if(!(result.get("code")==SysCode.error)){
		    cmsUser = cmsUserMapper.selectOne(cmsUser);
		    if(cmsUser!=null){		    	 
		    	 cmsUser.setState(Short.valueOf("1"));
		    	int count = cmsUserMapper.updateByPrimaryKeySelective(cmsUser);
		    	 if(count<1){//已经被注册直接返回
		    		 message.append("激活失败! ");		
				 }
		    }else{
		    	message.append("账号名密码错误! ");	
		    }  
		 }
		result.put("message", message.toString());
		return result;
	}
	/**
	 * 用户登录
	 * Created by sgy on 2020/6/9.
	 */
	@Override
	public Map<String, Object> login(Map<String, String> data) {
		Map<String, Object> result = ConmonConfig.getResultMap();	
		StringBuffer message = new StringBuffer(); 
		CmsUser cmsUser = new CmsUser();	
		//处理非空字段
		//用户名字不能为空
		if(StringUtils.isBlank(data.get("name"))){
			result.put("code",SysCode.error);
			message.append("用户名字不能为空! ");
		}else{
			cmsUser.setName(data.get("name"));
		}
		//密码不能为空
		if(StringUtils.isBlank(data.get("password"))){
		    result.put("code",SysCode.error);
			message.append("用户密码不能为空! ");			
		}else{
			cmsUser.setPassword(MD5Util.toMd5(data.get("password")));
		}
		if(!(result.get("code")==SysCode.error)){
			cmsUser = cmsUserMapper.selectOne(cmsUser);
			   if(cmsUser!=null){//账号密码校验成功
				   if(cmsUser.getState().intValue()==0){//尚未激活用户无法登陆
					   result.put("code",SysCode.error);
					   message.append("用户尚未激活无法登陆！  ");  
				  }else{
					  result.put("data", cmsUser);
				  }
			   }else{
				   result.put("code",SysCode.error);
				   message.append("账号密码不正确！  "); 
			   }	 
		 }
		result.put("message", message.toString());
		return result;
	}
	@Override
	public Map<String, Object> getUserById(String adminId) {
		Map<String, Object> result = ConmonConfig.getResultMap();	
		StringBuffer message = new StringBuffer(); 
		CmsUser cmsUser = new CmsUser();	
		if(StringUtils.isBlank(adminId)){
			result.put("code",SysCode.error);
			message.append("用户id不能为空! ");
		}
		cmsUser = cmsUserMapper.selectByPrimaryKey(Integer.parseInt(adminId));
		if(cmsUser!=null){//账号密码校验成功
			 result.put("data", cmsUser);
		 }else{
			 result.put("code",SysCode.error);
		   message.append("用户不存在！"); 
		 }
		result.put("message", message.toString());
		return result;
	}

	
	
	
   
    
  
}